﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_EaspLibs;

namespace V5_FrameWork.Data {
    public class BaseModelTopicCategory {
        string tableName = string.Empty;
        public BaseModelTopicCategory(string _tableName) {
            tableName = _tableName;
        }
        #region Model
        private int _classid;
        private string _classname;
        private int _parentid;
        /// <summary>
        /// 
        /// </summary>
        public int ClassID {
            set { _classid = value; }
            get { return _classid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ClassName {
            set { _classname = value; }
            get { return _classname; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ParentID {
            set { _parentid = value; }
            get { return _parentid; }
        }
        #endregion Model

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ClassID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + tableName + " where ClassID=" + ClassID + "");
            object obj = Easp.DB.ExecuteScalar(strSql.ToString());
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) {
                cmdresult = 0;
            }
            else {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0) {
                return false;
            }
            else {
                return true;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BaseModelTopicCategory model) {
            model.ClassID = GetMaxId();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + tableName + "(");
            strSql.Append("ClassID,ClassName,ParentID");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("" + model.ClassID + ",");
            strSql.Append("'" + model.ClassName + "',");
            strSql.Append("" + model.ParentID + "");
            strSql.Append(")");
            int result = Easp.DB.ExecuteNonQuery(strSql.ToString());
            return result;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(BaseModelTopicCategory model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + tableName + " set ");
            strSql.Append("ClassName='" + model.ClassName + "',");
            strSql.Append("ParentID=" + model.ParentID + "");
            strSql.Append(" where ClassID=" + model.ClassID + "");
            int result = Easp.DB.ExecuteNonQuery(strSql.ToString());
            return result;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ClassID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + tableName + " ");
            strSql.Append(" where ClassID=" + ClassID);
            Easp.DB.ExecuteNonQuery(strSql.ToString());
        }
        #endregion
    }
}
